package cn.eeepay.boss.action.aatAgentActivity;

import cn.eeepay.boss.system.DataSource;
import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.aatAgentActivity.*;
import cn.eeepay.framework.service.aatAgentActivity.AgentActivityService;
import cn.eeepay.framework.util.Constants;
import cn.eeepay.framework.util.ExportResult;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/agentActivityAction")
public class AgentActivityAction {

    private static final Logger log = LoggerFactory.getLogger(AgentActivityAction.class);

    @Resource
    private AgentActivityService agentActivityService;

    /**
     * 查询列表
     */
    @RequestMapping(value = "/selectAll")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectByParam(@RequestParam("info") String param, @ModelAttribute("page") Page<AgentActivity> page){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivity info = JSONObject.parseObject(param, AgentActivity.class);
            agentActivityService.selectAllList(info, page);
            msg.put("page",page);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询列表异常!");
        }
        return msg;
    }

    /**
     * 操作开关
     */
    @RequestMapping(value = "/saveSwitch")
    @ResponseBody
    @SystemLog(description = "活动-开关",operCode="agentActivityAction.saveSwitch")
    public Map<String,Object> saveSwitch(@RequestParam("id") int id,@RequestParam("sta") String sta){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            int num=agentActivityService.saveSwitch(id,sta);
            if(num>0){
                msg.put("msg","操作成功");
                msg.put("status", true);
            }else{
                msg.put("msg","操作失败");
                msg.put("status", false);
            }
        } catch (Exception e){
            log.error("操作开关异常!",e);
            msg.put("status", false);
            msg.put("msg", "操作开关异常!");
        }
        return msg;
    }

    /**
     * 新增代理商活动
     */
    @RequestMapping(value = "/addAgentActivity")
    @ResponseBody
    @SystemLog(description = "新增代理商活动",operCode="agentActivityAction.addAgentActivity")
    public Map<String,Object> addAgentActivity(@RequestParam("info") String param){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivity info = JSONObject.parseObject(param, AgentActivity.class);
            agentActivityService.addAgentActivity(info,msg);
        } catch (Exception e){
            log.error("新增代理商活动异常!",e);
            msg.put("status", false);
            msg.put("msg", "新增代理商活动异常!");
        }
        return msg;
    }

    /**
     * 获取代理商获得
     */
    @RequestMapping(value = "/getAgentActivity")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> getAgentActivity(@RequestParam("actCode") String actCode){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivity info=agentActivityService.getAgentActivity(actCode);
            msg.put("info",info);
            msg.put("status", true);
        } catch (Exception e){
            log.error("新增代理商活动异常!",e);
            msg.put("status", false);
            msg.put("msg", "新增代理商活动异常!");
        }
        return msg;
    }
    /**
     * 修改代理商活动
     */
    @RequestMapping(value = "/editAgentActivity")
    @ResponseBody
    @SystemLog(description = "修改代理商活动",operCode="agentActivityAction.editAgentActivity")
    public Map<String,Object> editAgentActivity(@RequestParam("info") String param){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivity info = JSONObject.parseObject(param, AgentActivity.class);
            agentActivityService.editAgentActivity(info,msg);
        } catch (Exception e){
            log.error("修改代理商活动异常!",e);
            msg.put("status", false);
            msg.put("msg", "修改代理商活动异常!");
        }
        return msg;
    }

    /**
     * 删除代理商活动
     */
    @RequestMapping(value = "/deleteAgentActivity")
    @ResponseBody
    @SystemLog(description = "删除代理商活动",operCode="agentActivityAction.deleteAgentActivity")
    public Map<String,Object> deleteAgentActivity(@RequestParam("actCode") String actCode){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            agentActivityService.deleteAgentActivity(actCode,msg);
        } catch (Exception e){
            log.error("修改代理商活动异常!",e);
            msg.put("status", false);
            msg.put("msg", "修改代理商活动异常!");
        }
        return msg;
    }

    /**
     * 获取子活动数据
     */
    @RequestMapping(value = "/getAgentActivityDetail")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> getAgentActivityDetail(@RequestParam("actDetCode") String actDetCode){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivityDetail info=agentActivityService.getAgentActivityDetail(actDetCode);
            msg.put("info",info);
            msg.put("status", true);
        } catch (Exception e){
            log.error("新增代理商活动异常!",e);
            msg.put("status", false);
            msg.put("msg", "新增代理商活动异常!");
        }
        return msg;
    }

    /**
     * 保存子活动数据
     */
    @RequestMapping(value = "/editAgentActivityDetail")
    @ResponseBody
    @SystemLog(description = "保存子活动数据",operCode="agentActivityAction.editAgentActivityDetail")
    public Map<String,Object> editAgentActivityDetail(@RequestParam("info") String param){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivityDetail info = JSONObject.parseObject(param, AgentActivityDetail.class);
            agentActivityService.editAgentActivityDetail(info,msg);
        } catch (Exception e){
            log.error("保存子活动数据异常!",e);
            msg.put("status", false);
            msg.put("msg", "保存子活动数据异常!");
        }
        return msg;
    }


    /**
     * 查询活动数据
     */
    @RequestMapping(value = "/selectAgentActivityData")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectAgentActivityData(@RequestParam("info") String param, @ModelAttribute("page") Page<AgentActivityData> page){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivityData info = JSONObject.parseObject(param, AgentActivityData.class);
            agentActivityService.selectAgentActivityData(info,page);
            msg.put("page",page);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询活动数据列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询活动数据列表异常!");
        }
        return msg;
    }
    /**
     * 导出活动数据
     */
    @RequestMapping(value = "/exportActivityData")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @SystemLog(description = "导出活动数据",operCode="agentActivityAction.exportActivityData")
    public Map<String,Object> exportActivityData(@RequestParam("info") String param, HttpServletResponse response){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivityData info = JSONObject.parseObject(param, AgentActivityData.class);
            agentActivityService.exportActivityData(info,response);
        } catch (Exception e){
            ExportResult.setExportResult(response,false,"导出异常");
            log.error("导出活动数据!",e);
        }
        return msg;
    }

    /**
     * 查询排行版列表
     */
    @RequestMapping(value = "/selectAgentRankingList")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectAgentRankingList(@RequestParam("info") String param, @ModelAttribute("page") Page<AgentActivityData> page){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivityData info = JSONObject.parseObject(param, AgentActivityData.class);
            agentActivityService.selectAgentRankingList(info, page,msg);
        } catch (Exception e){
            log.error("查询排行版列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询排行版列表异常!");
        }
        return msg;
    }

    /**
     * 导出排行版数据
     */
    @RequestMapping(value = "/exportAgentRankingList")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @SystemLog(description = "导出排行版数据",operCode="agentActivityAction.exportAgentRankingList")
    public Map<String,Object> exportAgentRankingList(@RequestParam("info") String param, HttpServletResponse response){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AgentActivityData info = JSONObject.parseObject(param, AgentActivityData.class);
            agentActivityService.exportAgentRankingList(info,response);
        } catch (Exception e){
            ExportResult.setExportResult(response,false,"导出异常");
            log.error("导出导出排行版数据!",e);
        }
        return msg;
    }
}
